package com.example.yckjbigdataservice.config;

/*import com.example.yckjbigdataservice.properties.ElasticsearchProperties;
import com.huawei.fusioninsight.elasticsearch.transport.client.ClientFactory;
import com.huawei.fusioninsight.elasticsearch.transport.client.PreBuiltHWTransportClient;*/
import com.example.yckjbigdataservice.properties.ElasticsearchProperties;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.hwclient.HwRestClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.annotation.Order;

import java.io.File;
import java.io.IOException;

@Configuration
@Slf4j
@Order(52)
public class ElasticsearchConfig {

    @Value("${custom.elasticsearch.config-path}")
    private String configPath;

    @Value("${custom.elasticsearch.config-name}")
    private String configName;

    @Value("${custom.elasticsearch.principal}")
    private String principal;

    @Value("${custom.elasticsearch.user-keytab.file.path}")
    private String userKeytabFilePath;

    @Value("${custom.elasticsearch.kbr5.file.path}")
    private String kbr5FilePath;

    @Value("${server.port}")
    private int port;

    @Value("${spring.application.name}")
    private String appName;

    @Bean
    @DependsOn(value = "kafkaProducer")
    /*public PreBuiltHWTransportClient preBuiltHWTransportClient() throws IOException {
        ClientFactory.initConfiguration(ElasticsearchProperties.loadProperties(configPath,
                principal, kbr5FilePath, userKeytabFilePath,
                System.getProperty("user.dir")+ File.separator+"jaas"+ File.separator + appName+port+"es.jaas.conf"
        ));
        return ClientFactory.getClient();
    }*/
    public RestHighLevelClient hwRestClient(){
        ElasticsearchProperties elasticsearchProperties =
                new ElasticsearchProperties(configPath, configName, principal, userKeytabFilePath, kbr5FilePath,
                        System.getProperty("user.dir")+ File.separator+"jaas"+ File.separator + appName+port+".jaas.conf");
        //HwRestClient hwRestClient =  new HwRestClient(configPath, configName);
        return new RestHighLevelClient(elasticsearchProperties.getRestClientBuilder());
    }

}
